<template>
  <div class="article-details">
    <div class="container">
      <!-- 左侧：文章信息 -->
      <div class="article-section">
        <div class="article-header">
          <h2>{{ article.title }}</h2>
          <img v-if="article.user_avatar" :src="article.user_avatar" class="user-avatar" />
          <div class="article-meta">
            <span class="article-author">{{ article.user_nickname }}</span>
            <span class="article-time">{{ formattedCreatedTime }}</span>
          </div>
        </div>
        <div class="article-content">
          {{ article.content_text }}
        </div>
      </div>

      <!-- 右侧：评论信息 -->
      <div class="comments-section">
        <h3>评论</h3>
        <div class="comments-list">
          <div class="comment" v-for="comment in article.comments" :key="comment.comment_id">
            <img v-if="comment.user_avatar" :src="comment.user_avatar" class="comment-avatar" />
            <div class="comment-content">
              <strong>{{ comment.user_nickname }}</strong>
                {{ comment.content }}
              <div class="comment-meta">
                <span class="comment-time">{{ formattedPublishTime(comment.publish_time) }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    article: {
      type: Object,
      required: true,
    },
  },
  computed: {
    formattedCreatedTime() {
      return new Date(this.article.created_time).toLocaleString();
    },
  },
  methods: {
    formattedPublishTime(time) {
      return new Date(time).toLocaleString();
    },
  },
};
</script>

<style scoped>
.article-details {
  padding: 16px;
  border: 1px solid #ccc;
  border-radius: 8px; /* 增加圆角 */
  display: flex;
  justify-content: center; /* 水平居中 */
}

.container {
  display: flex;
  width: 100%;
  max-width: 1200px; /* 限制最大宽度 */
  margin: 0 auto; /* 水平居中 */
  gap: 24px; /* 左右间距 */
}

/* 左侧：文章信息 */
.article-section {
  flex: 3;
  display: flex;
  flex-direction: column;
  padding-right: 24px; /* 增加右侧间距 */
  overflow-y: auto; /* 添加垂直滚动 */
  max-height: 600px; /* 设置最大高度以匹配评论区域 */
}

.article-header {
  display: flex;
  align-items: center;
  gap: 12px; /* 图片与标题间距 */
  margin-bottom: 24px; /* 增加底部间距 */
}

.user-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
}

.article-meta {
  font-size: 14px;
  color: #666;
  display: flex;
  align-items: center;
  gap: 8px; /* 作者与时间间距 */
}

.article-content {
  line-height: 1.8;
  color: #333;
  font-size: 16px; /* 增加字体大小 */
  overflow-y: auto; /* 添加垂直滚动 */
  max-height: 400px; /* 根据需要调整 */
  padding-right: 8px; /* 为溢出内容留出空间 */
}

/* 右侧：评论信息 */
.comments-section {
  flex: 1;
  padding-left: 24px; /* 增加左侧间距 */
  border-left: 1px solid #eee; /* 增加分隔线 */
  max-height: 600px; /* 设置与文章信息相同的最大高度 */
  overflow-y: auto; /* 添加垂直滚动 */
}

.comments-section h3 {
  margin-bottom: 12px;
  color: #333;
  font-size: 18px; /* 增加字体大小 */
}

.comments-list {
  display: flex;
  flex-direction: column;
  gap: 16px; /* 评论间距 */
}

.comment {
  display: flex;
  align-items: flex-start;
  gap: 12px; /* 头像与评论内容间距 */
}

.comment-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.comment-content {
  flex-grow: 1;
}

.comment-content p {
  margin: 4px 0;
  color: #555;
}

.comment-meta {
  font-size: 12px;
  color: #999;
  margin-top: 4px;
}

/* 响应式调整 */
@media (max-width: 992px) {
  .container {
    flex-direction: column;
    gap: 16px;
  }

  .article-section,
  .comments-section {
    max-height: none;
    padding-right: 0;
    padding-left: 0;
    border-left: none;
  }

  .article-content {
    max-height: none;
  }
}
</style>
